/**** TABLE I: SUMMARY STATISTICS**********/

use ../data/police_killings, clear
keep if sample_main==1

// summary stats for text
count
codebook sht_id incident_id 
preserve
bys incident_id (sht_id): keep if _n==1
duplicates report cb
restore

// make table 1
gen all = 1
gen black_hisp 	=  sht_black==1 | sht_hispanic==1
gen white_asian =  sht_white==1 | sht_asian==1
foreach g in all black_hisp white_asian {
	cap mat drop `g'_1
	foreach v in age sht_black sht_hispanic sht_white sht_asian sht_male {
		sum `v' if `g'==1
		mat `g'_1 = nullmat(`g'_1) \ round(r(mean),0.00001)
	}
	cap mat drop `g'_2
	foreach v in any_mentions ttl_mentions  {
		sum `v' if `g'==1
		mat `g'_2 = nullmat(`g'_2) \ round(r(mean),0.00001)
	}
	sum ttl_mentions if `g'==1 & any_mentions,det
	mat `g'_2 = nullmat(`g'_2) \ r(p50)
	cap mat drop `g'_3
	foreach v in weap_unarmed weap_knife weap_gun weap_miss {
		sum `v' if `g'==1
		mat `g'_3 = nullmat(`g'_3) \ round(r(mean),0.00001)
	}
	count if `g'==1
	mat N_`g' = r(N)
	cap mat drop `g'
	mat p1_`g' =  (`g'_1 \ . \ . \ `g'_2 \ . \ . \ `g'_3 \ .\ N_`g')
}
mat p1 = p1_all , p1_black_hisp , p1_white_asian 
mat list p1

use ../data/main, clear
keep if sample_main==1 & elec_yr==2002
// br cb elec_yr all_c00 reg vote
gen reg_per18 = reg / all_c00
gen voter_turnout = vote / reg
gen vote_per18 = vote / all_c00
foreach v in black hisp whiteasian dem rep 1834 3554 0to3 {
	gen reg_pct_`v' = reg_`v'/reg
}
bysort CBG: egen treat_cbg = max(treat)
assert all ==1
gen cbg_not = (treat!=1 & treat_cbg==1)
foreach g in all treat cbg_not {
	cap mat drop p2_`g'_1
	foreach v in all_c00 pct_black pct_hisp pct_white pct_api  {
		sum `v' if `g'==1
		mat p2_`g'_1 = nullmat(p2_`g'_1) \ round(r(mean),0.00001)
	}
	cap mat drop p2_`g'_2
	foreach v in reg_pct_black reg_pct_hisp reg_pct_whiteasian reg_pct_1834 ///
		reg_pct_3554 reg_pct_dem reg_pct_rep reg_pct_0to3 reg_per18 voter_turnout {
		sum `v' if `g'==1
		mat p2_`g'_2 = nullmat(p2_`g'_2) \ round(r(mean),0.00001)
	}
	count if `g'==1
	mat N2_`g' = r(N)
	cap mat drop p2_`g'
	mat p2_`g' = p2_`g'_1  \ . \ . \ p2_`g'_2 \ .\ N2_`g'
}
mat p2 = p2_all ,  p2_treat , p2_cbg_not
mat list p2
mat spacer = (.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.)
mat table1_sum = p1 , spacer, p2
mat list table1_sum

**** Export matrix code
cap prog drop fill_tables
program fill_tables
	syntax, mat(str) save_excel(str) sheet(str) cell(str)
	foreach tab in `mat' {
		clear
		matrix list `tab'
		svmat2 `tab', rnames(var_names)
		export excel _all using "`save_excel'", firstrow(var) ///
			cell(`cell') sheetmodify sheet(`sheet')
	}
end

fill_tables, mat(table1_sum) save_excel("../output/table1_sum.xlsx") sheet(table1_sum) cell(L7)








// ../output/table1_sum.csv



// eststo: estpost sum age sht_black sht_hispanic sht_white sht_asian sht_male any_mentions ttl_mentions weap_unarmed weap_knife weap_gun weap_miss
//
// eststo: estpost sum sht_black sht_hispanic sht_white sht_asian sht_male age any_mentions ttl_mentions weap_unarmed weap_knife weap_gun weap_miss if sht_black==1 | sht_hispanic==1
//
// eststo: estpost sum sht_black sht_hispanic sht_white sht_asian sht_male age any_mentions ttl_mentions weap_unarmed weap_knife weap_gun weap_miss if sht_white==1 | sht_asian==1
// esttab using ../output/table1_sum.csv, replace cells("mean") csv 
// eststo clear
//
// eststo: estpost sum all_c00 pct_black pct_hisp  pct_white pct_api reg_pct_black reg_pct_hisp reg_pct_whiteasian reg_pct_1834 reg_pct_3554 reg_pct_dem reg_pct_rep reg_pct_0to3 reg_per18 voter_turnout
//
// eststo: estpost sum all_c00 pct_black pct_hisp  pct_white pct_api reg_pct_black reg_pct_hisp reg_pct_whiteasian reg_pct_1834 reg_pct_3554 reg_pct_dem reg_pct_rep reg_pct_0to3 reg_per18 voter_turnout if treat
//
// eststo: estpost sum all_c00 pct_black pct_hisp  pct_white pct_api reg_pct_black reg_pct_hisp reg_pct_whiteasian reg_pct_1834 reg_pct_3554 reg_pct_dem reg_pct_rep reg_pct_0to3 reg_per18 voter_turnout if !treat & treat_cbg
//
// esttab using ../output/table1_sum.csv, append cells("mean") csv 
